package com.group01.community.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group01.community.entity.SysDictData;
import com.group01.community.entity.SysPost;
import com.group01.community.service.SysPostService;
import com.group01.community.utils.ExcelUtils;
import com.group01.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author iuoly
 * @since 2022-07-04
 */
@Api
@RestController
@RequestMapping("/sysPost")
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseResult<String> add(@RequestBody SysPost sysPost) {
        sysPost.setCreateTime(LocalDateTime.now());
        sysPostService.save(sysPost);
        return ResponseResult.ok("添加成功");
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/{postIds}")
    public ResponseResult<String> delete(@PathVariable Long[] postIds) {
        sysPostService.removeByIds(Arrays.asList(postIds));
        return ResponseResult.ok("删除成功");
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseResult<String> update(@RequestBody SysPost sysPost) {
        sysPostService.updateById(sysPost);
        return ResponseResult.ok("修改成功");
    }

    @ApiOperation("按条件查询")
    @PostMapping("/condition")
    public ResponseResult<IPage<SysPost>> selectCondition(@RequestBody SysPost sysPost, Long current, Long size) {
        return ResponseResult.ok(sysPostService.selectCondition(sysPost, current, size));
    }

    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public ResponseResult<SysPost> selectSysPostById(@PathVariable Long id) {
        return ResponseResult.ok(sysPostService.getById(id));
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public ResponseResult<String> export(HttpServletResponse resp) {
        ExcelUtils.export(SysPost.class, sysPostService, resp, "岗位信息");
        return ResponseResult.ok("ok");
    }
}

